import { Controller, Post, Body, HttpCode, HttpStatus } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBody } from '@nestjs/swagger';
import { WalletAuthService } from '../services/wallet-auth.service';
import { WalletAuthDto } from '../dto/wallet-auth.dto';
import { AuthResponseDto } from '../dto/auth-response.dto';
import { ApiResponseDto } from '../../common/dto/api-response.dto';
import { AppI18nService } from '../../i18n/i18n.service';
import { CurrentLanguage } from '../../i18n/language.decorator';

@ApiTags('auth')
@Controller('auth')
export class AuthController {
    constructor(
        private readonly walletAuthService: WalletAuthService,
        private readonly appI18nService: AppI18nService
    ) { }

    @Post('wallet-login')
    @HttpCode(HttpStatus.OK)
    @ApiOperation({
        summary: 'Authenticate user with wallet signature',
        description: 'Authenticates a user by verifying their Ethereum wallet signature. If the user does not exist, a new user account will be created.'
    })
    @ApiBody({
        type: WalletAuthDto,
        description: 'Wallet authentication credentials'
    })
    @ApiResponse({
        status: 200,
        description: 'Authentication successful',
        type: ApiResponseDto<AuthResponseDto>
    })
    @ApiResponse({
        status: 401,
        description: 'Invalid signature or authentication failed',
        type: ApiResponseDto<null>
    })
    @ApiResponse({
        status: 400,
        description: 'Invalid request data',
        type: ApiResponseDto<null>
    })
    async walletLogin(
        @Body() walletAuthDto: WalletAuthDto,
        @CurrentLanguage() language: string
    ): Promise<ApiResponseDto<AuthResponseDto>> {
        const authResult = await this.walletAuthService.walletLogin(walletAuthDto, language);
        const message = this.appI18nService.translate('auth.login_success', { lang: language });
        return ApiResponseDto.success(authResult, message);
    }
}